import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import { LoginService } from '../login/common-service/login.service';
import { ActivatedRoute, Router } from '@angular/router';
import * as _ from 'lodash';
import { TranslateService } from '@ngx-translate/core';
import { MenuItem } from 'primeng/components/common/api';
import { UserService } from '../shared/services/user.service';
@Component({
  selector: 'app-agent',
  templateUrl: './agent.component.html',
  styleUrls: ['./agent.component.css']
})
export class AgentComponent implements OnInit {
  display: boolean = false;
  newPassword1: string = "";
  newPassword2: string = "";
  user: any = {};
  tabMenuItems: MenuItem[];
  constructor(private route: ActivatedRoute,
    private router: Router,
    private loginService: LoginService,
    private translate: TranslateService,
    private userService: UserService) { }
  ngOnInit() {
    this.tabMenuItems = [
      {label: 'Stats', icon: 'ui-icon-insert-chart'},
      {label: 'Calendar', icon: 'ui-icon-date-range'},
      {label: 'Documentation', icon: 'ui-icon-book'},
      {label: 'Support', icon: 'ui-icon-help-outline'},
      {label: 'Social', icon: 'ui-icon-public'}
  ];
    this.getCurrentUser();
  }

  //logout
  onLoggedout() {
    this.userService.logout().subscribe(
      (res) => {
        this.router.navigate(['/login']);
      },
      (err) => {
        console.log(err);
        // this.router.navigate(['/login']);
      }
    )
  }

  resetPassword() {
    if (this.newPassword1 === this.newPassword2) {
      this.userService.patch(this.user.id, { password: this.newPassword1 }).subscribe(
        (res) => {
          console.log(res);
          alert("update password success!");
          this.display = false;
        },
        (err) => {
          alert("update password fail!");
        }
      );
    } else {
      alert("the two passwords not match!");
    }

  }

  getCurrentUser() {
    this.userService.getCurrentUser().subscribe((user) => {
      this.user = user;
      console.log('agent login get current user', this.user);
    });
  }

  editProfile(){
    this.router.navigate(['/agent/shared/profile/',this.user.id]);
  }


  changeLang(language: string) {
    this.translate.use(language);
  }


}
